package com.lsh.payment.core.strategy.bill;

import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.core.constant.BusinessConstant;
import com.lsh.payment.core.exception.BusinessException;
import com.lsh.payment.core.model.payenum.BillTradeType;
import com.lsh.payment.core.strategy.config.WxPayConfig;
import com.lsh.payment.core.util.*;
import com.lsh.payment.core.util.pay.wxpay.WxSignature;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: lsh-payment
 * Created by peter on 16/11/24.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class WxGrouponBillStrategy implements BillStrategy {

    private Logger logger = LoggerFactory.getLogger(WxGrouponBillStrategy.class);

//    public static String url = "https://api.mch.weixin.qq.com/pay/downloadbill";

    /**
     * 下载对账单
     *
     * @param billDate 查询参数
     * @param wxType   对账单类型
     * @return boolean
     */
    @Override
    public boolean downloadBillByDate(String billDate, int wxType) {
        try {

            if(StringUtils.isBlank(billDate)){
                billDate = DateUtil.getYesterdayStr();
            }

            String wxpayKey = "";
            String billFileName = "";
            String downLoadBillPath = BusinessConstant.BILL_DOWNLOAD_PATH_WX_GROUPON + File.separator + DateUtil.getDateString();
            PathUtil.createPath(downLoadBillPath);

            Map<String, String> wxPayRequestBody = new HashMap<>(10);
            // TODO 2019-12-05
//            if (BillTradeType.JSAPI.getCode() == wxType) {
//                //微信公众号
//                logger.info("wx bill downLoad type is : " + BillTradeType.JSAPI.getName());
//                // 应用ID
//                wxPayRequestBody.put("appid", WxPayConfig.GROUPON_APPID);
//                // 商户号
//                wxPayRequestBody.put("mch_id", WxPayConfig.GROUPON_MCHID);
//                wxpayKey = WxPayConfig.GROUPON_KEY;
//                billFileName = MessageFormat.format(WxPayConfig.WX_H5_BILL_NAME, billDate);
//
//            }

            wxPayRequestBody.put("nonce_str", MD5Util.generateNonceStr());
            wxPayRequestBody.put("bill_date", billDate);
            wxPayRequestBody.put("bill_type", "SUCCESS");
            //设置签名
            wxPayRequestBody.put("sign", WxSignature.getNewSign(wxPayRequestBody, wxpayKey));
            //请求xml字符串
            String wxPayRequestXml = XmlUtil.toXml(wxPayRequestBody);
            String wxPayResponseXml = HttpClientUtils.doPostXmlBill(WxPayConfig.WX_BILL_DOWNLOAD_URL, wxPayRequestXml, downLoadBillPath, billFileName);

            logger.info("wx bill downLoad wxPayResponseXml is : " + wxPayResponseXml);

        } catch (Throwable e) {
            e.printStackTrace();
            if (wxType == BillTradeType.JSAPI.getCode()) {
                throw new BusinessException(ExceptionStatus.E2004001.getCode(), ExceptionStatus.E2004001.getMessage());
            } else if (wxType == BillTradeType.APP.getCode()) {
                throw new BusinessException(ExceptionStatus.E2004002.getCode(), ExceptionStatus.E2004002.getMessage());
            }

            e.printStackTrace();
        }
        
        return true;
    }
}
